package com.io.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.io.dto.ProductListRes;
import com.io.dto.ProductReq;
import com.io.dto.ProductRes;
import com.io.service.ProductServiceI;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;

/**
 * The class/interface
 *
 * @author guodd
 * @version 1.0 use jdk 1.8
 */
@RestController
@RefreshScope
@Slf4j
public class ProductController {
    @Value(value = "${product.profile}")
    private String profile;

    @Autowired
    private ProductServiceI productServiceI;

    @GetMapping(value = "getInfo1")
    @SentinelResource(value = "getInfo")
    public String getInfo1() {
        log.info("[{}]", "java");
        return profile;
    }

    @GetMapping(value = "getInfo2")
    public String getInfo2() {
        return profile;
    }

    @GetMapping(value = "getInfo3")
    public ProductListRes getInfo3() {
        ProductListRes productListRes = new ProductListRes();
        productListRes.setTotal(20L);
        productListRes.setData(Collections.singletonList(new ProductRes()));
        return productListRes;
    }

    @PostMapping(value = "/product/info")
    public ProductRes product(@RequestBody ProductReq productReqDTO) {
        return productServiceI.findById(productReqDTO);
    }
}
